package com.hjx.controller;

import com.hjx.entity.SysMenu;
import com.hjx.entity.SysRoleMenu;
import com.hjx.logging.LogAnnotation;
import com.hjx.response.AjaxResponse;
import com.hjx.response.ResponseType;
import com.hjx.service.SysRoleMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2020/6/17 - 9:42
 */
@RestController
@RequestMapping("rm")
public class SysRoleMenuController {
    @Resource
    SysRoleMenuService sysRoleMenuService;

    //根据角色ID查询角色拥有的权限
    @LogAnnotation
    @GetMapping("/findMenuByRid/{rid}")
    public AjaxResponse findMenuByRid(@PathVariable String rid){
        List<SysMenu> menus = sysRoleMenuService.findMenuByRid(rid);
        if (menus!=null){
            return new AjaxResponse(ResponseType.QUERY_SUCCESS,menus);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }
    //根据角色ID查询角色没有的权限
    @LogAnnotation
    @GetMapping("/findNoGetMenuByRid/{rid}")
    public AjaxResponse findNoGetMenuByRid(@PathVariable String rid){
        List<SysMenu> menus = sysRoleMenuService.findNoGetMenuByRid(rid);
        if (menus!=null){
            return new AjaxResponse(ResponseType.QUERY_SUCCESS,menus);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }
    //为角色添加权限 可以同时添加多个权限
    @LogAnnotation
    @PostMapping("insertBatch")
    public AjaxResponse insertBatch(@RequestBody SysRoleMenu sysRoleMenu){
        boolean result = sysRoleMenuService.insertBatch(sysRoleMenu);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }
    //删除角色的权限 可以同时添加多个角色
    @LogAnnotation
    @PostMapping("delete")
    public AjaxResponse delete(@RequestBody SysRoleMenu sysRoleMenu){
        boolean result = sysRoleMenuService.delete(sysRoleMenu);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }
}
